package models.user;

import models.*;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;

import models.pubobj.AniObjectState;

import java.util.*;

import javax.persistence.*;

/**
 * User: yeqingzhou
 * Date: 12-8-30
 */
@Entity
public class AniUser extends Model {
	
    /**
	 * 
	 */
	private static final long serialVersionUID = -6219482480876625851L;

	@Id
    public Long id;
    
    @Constraints.Required
    @Formats.NonEmpty
    public String email;

    @Constraints.Required
    public String password;
    
    @Constraints.Required
    public String screenName;
    
    @Constraints.Required
    @ManyToOne
    public AniObjectState state;
    
    @Constraints.Required
    @ManyToOne
    public AniUserRole role;
    
    @Lob
    public byte[] avatar;
    
    public AniUser(){}
    
    public AniUser(String email, String password, AniObjectState state,
			AniUserRole role, String screenName) {
		super();
		this.email = email;
		this.password = password;
		this.state = state;
		this.role = role;
		this.screenName = screenName;
	}
    public AniUser(Long id, String email, String password, AniObjectState state,
			AniUserRole role, String screenName) {
		super();
		this.id = id;
		this.email = email;
		this.password = password;
		this.state = state;
		this.role = role;
		this.screenName = screenName;
	}

	public AniUser(Long id, String password, String screenName) {
		super();
		this.id = id;
		this.password = password;
		this.screenName = screenName;
	}



	public static Finder<Long, AniUser> find = new Finder<Long, AniUser>(Long.class, AniUser.class);

    /**
     * Retrieve all users.
     */
    public static List<AniUser> findAll() {
        return find.all();
    }

    /**
     * Retrieve a User from email.
     */
    public static AniUser findByEmail(String email) {
        AniUser oneUser = find.where().eq("email", email).findUnique();
        return oneUser;
    }
    
    /**
     * Retrieve a User from screenName.
     */
    public static AniUser findByScreenName(String screenName) {
        //return find.fetch("state").fetch("role").where().eq("email", email).findUnique();
        return find.where().eq("screenName", screenName).findUnique();
    }

    /**
     * Authenticate a User.
     */
    public static AniUser authenticate(String email, String password) {
    	return find.where()
                .eq("email", email)
                .eq("password", password)
                .findUnique();
    }
    
    

}
